﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;

namespace Infrastructure.Domain
{
    public interface INameValuePair
    {
        string Name { get; set; }
        string Value { get; set; }
    }

    public class NameValuePair : INameValuePair
    {

        public string Name { get; set; }
        public string Value { get; set; }
        public NameValuePair()
        { }
        public NameValuePair(string name, string value)
        {
            Name = name;
            Value = value;
        }
        public override string ToString()
        {
            return Value;
        }

    }

    public static class NameValuePairExtensions
    {
        public static void Add(this List<INameValuePair> list, string key, string value)
        {
            list.Add(new NameValuePair(key, value));
        }
        public static void SetValue(this List<INameValuePair> list, string key, string value)
        {
            if (list.ContainsKey(key))
            {
                INameValuePair nvp = list.Where(e => e.Name == key).FirstOrDefault();
                list.Remove(nvp);
            }
        }
        public static bool ContainsValue(this List<INameValuePair> list, string value)
        {
            return list.ToSafeList().Any(c => c.Value == value);
        }
        public static bool ContainsKey(this List<INameValuePair> list, string key)
        {
            return list.ToSafeList().Any(c => c.Name == key);
        }
        public static bool Contains(this List<INameValuePair> list, string key, string value)
        {
            if (list.ContainsKey(key))
            {
                return list.ToSafeList().Where(e => e.Name == key && e.Value.Contains(value)).Any();
            }
            return false;
        }
        public static List<INameValuePair> Without(this List<INameValuePair> list, string key)
        {
            return list.ToSafeList().Where(e => e.Name != key).ToList();
        }
        public static List<INameValuePair> ToINameValuePairList(this Dictionary<string, string> items)
        {
            if (items == null)
                return new List<INameValuePair>();

            List<INameValuePair> ret = items.Select(e => (INameValuePair)new NameValuePair(e.Value, e.Key)).ToList();
            return ret;


        }
        public static List<INameValuePair> ToINameValuePairList(this Type enumeration)
        {

            if (enumeration.BaseType != typeof(Enum))
            {
                throw new ArgumentException("enumeration must be an enum"
                    );
            }
            List<INameValuePair> ret = Enum.GetNames(enumeration)
                            .Select(e => (INameValuePair)new NameValuePair(e.ToFriendlyString(), ((int)Enum.Parse(enumeration, e)).ToString()))
                            .ToList();
            return ret;
        }
        public static SelectList AsSelectList(this List<INameValuePair> list, string selectedValue = null)
        {
            return new SelectList(list, "Value", "Name", selectedValue);
        }
        public static List<INameValuePair> ToINameValuePairListWithUnfriendlyNames(this Type enumeration)
        {

            if (enumeration.BaseType != typeof(Enum))
            {
                throw new ArgumentException("enumeration must be an enum"
                    );
            }
            List<INameValuePair> ret = Enum.GetNames(enumeration)
                            .Select(e => (INameValuePair)new NameValuePair(e, ((int)Enum.Parse(enumeration, e)).ToString()))
                            .ToList();
            return ret;
        }
        public static string GetValue(this IEnumerable<INameValuePair> list, string name)
        {
            return (list.Where(e => e.Name == name).SingleOrDefault()).ToSafeString();
        }
        public static IEnumerable<INameValuePair> AsEnumerable(this NameValueCollection nvc)
        {
            List<INameValuePair> ret = new List<INameValuePair>();
            for (int i = 0; i < nvc.Count; i++)
            {
                ret.Add(new NameValuePair(nvc.Keys[i], nvc[nvc.Keys[i]]));
            }
            return ret.AsEnumerable();
        }


    }
}
